/// <reference types="cypress" />
describe('我的已提交', () => {
    // after(()=>{
    //     cy.logout();
    // });

    context('查询测试', () => {
        before(() => {
            cy.login('dc\\LuJuan1', 'devdev');
        });

        it('查询条件测试', () => {
            cy.visit('/Process/MyWorkflow/Submits');
            cy.wait(500);
            cy.get('.search-header > .extra').click();

            let current = new Date();
            let begin = new Date(current.getFullYear(), current.getMonth() - 3, current.getDate() - 10);

            cy.get('#txtProcessStartDateBegin').clear().type(`${begin.getFullYear()}-${begin.getMonth() + 1}-${begin.getDate()}`);

            // cy.log('打开流程类型选择对话框...');
            // cy.get('div[data-text-id=txtProcesses] > .btn-group > .btn:first').click();
            // cy.wait(600);

            // cy.get('iframe').then($iframe => {
            //     cy.get($iframe.contents()).as('iframe')
            //     cy.get('@iframe').find('#keywords').type('clsa4.0');

            //     cy.wait(1200);
            //     cy.get('@iframe').find('.op-buttons > .btn').eq(2).click();
            //     cy.get('.layui-layer-btn > .layui-layer-btn0').click();

            //     cy.wait(1000);
            //     cy.log('查询所有猎豹用印单据');
            //     cy.get('#frmSearch > .buttons > .btn-primary').click();

            //     cy.log('检查返回结果是否正确：');
            //     cy.get('.layui-table-main > table > tbody > tr').then($items => {
            //         expect($items.length).to.lte(10, '每页显示记录数不超过10');

            //         $items.each(function () {
            //             expect(Cypress.$(this).find('td:eq(1)').text()).to.contains('CLSA4.0', '是用印申请单据')
            //         });
            //     });
            // });
        });

        it('分页测试', () => {
            cy.get('#Submits > .layui-form > div').eq(0).should('have.class', 'layui-table-page');

            cy.get('#Submits > .layui-form > .layui-table-page .layui-laypage').then($pgContainer => {
                let pages = parseInt(Cypress.$($pgContainer).find('.layui-laypage-last').text()) || 1;
                let countText = Cypress.$($pgContainer).find('.layui-laypage-count').text();
                let count = parseInt(countText.replace('共 ', '').replace('条'));

                cy.log(`总页数：${pages}，总记录数：${count}`);

                for (let index = 0; index < pages; index++) {
                    let first = (index * 10 + 1).toString();
                    let last = (index + 1 == pages ? count : index * 10 + 10).toString();
                    
                    cy.get('.layui-table-main > table > tbody > tr').then($datas => {
                        expect(Cypress.$($datas).first().find('>td:eq(0)').text()).to.equal(first, '起始的序号验证');
                        expect(Cypress.$($datas).last().find('>td:eq(0)').text()).to.equal(last, '结束的序号验证');
                    });

                    if (index + 1 >= pages) {
                        break;
                    }

                    let nextPage = index + 2;

                    cy.get('#Submits .layui-laypage-skip > .layui-input').clear().type(nextPage.toString());
                    cy.wait(200);
                    cy.get('#Submits .layui-laypage-btn').click();
                    cy.wait(1000);
                }
            });
        });
    });

});